package com.xiaoge.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoge.auth.security.UserInfo;
import com.xiaoge.biz.entity.*;
import com.xiaoge.biz.mapper.OutStockInfoMapper;
import com.xiaoge.biz.mapper.OutStockMapper;
import com.xiaoge.biz.mapper.ProductStockMapper;
import com.xiaoge.biz.service.OutStockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoge.biz.vo.GetOutStockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiaoge
 * @since 2021-03-21
 */
@Service
public class OutStockServiceImpl extends ServiceImpl<OutStockMapper, OutStock> implements OutStockService {
    @Autowired
    private OutStockMapper outStockMapper;
    @Autowired
    private OutStockInfoMapper outStockInfoMapper;
    @Autowired
    private ProductStockMapper productStockMapper;

    @Override
    public IPage<OutStock> findOutStockList(Page<OutStock> page, QueryWrapper<OutStock> queryWrapper) {
        return baseMapper.findOutStockList(page, queryWrapper);
    }

    @Override
    public void addOutStock(GetOutStockVo getOutStockVo) {
        //随机生成出库单号
        String OUT_STOCK_NUM = UUID.randomUUID().toString().substring(0, 32);
        int itemNumber = 0;//记录该单的总数
        List<OutStockInfo> products = getOutStockVo.getProducts();
        //添加入库明细
        if (!ObjectUtils.isEmpty(products)) {
            for (OutStockInfo product : products) {
                itemNumber += product.getProductNumber();
                product.setOutNum(OUT_STOCK_NUM);
                outStockInfoMapper.insert(product);
            }
        }
        OutStock outStock = new OutStock();
        outStock.setOutNum(OUT_STOCK_NUM);
        outStock.setProductNumber(itemNumber);
        outStock.setPriority(getOutStockVo.getPriority());
        outStock.setStatus(2);
        outStock.setRemark(getOutStockVo.getRemark());
        outStock.setType(getOutStockVo.getType());
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (!(authentication instanceof AnonymousAuthenticationToken)) {
            UserInfo userInfo = (UserInfo) authentication.getPrincipal();
            outStock.setOperator(userInfo.getUsername());
        }
        if (getOutStockVo.getConsumerId() != null) {
            outStock.setConsumerId(getOutStockVo.getConsumerId());
        }
        outStockMapper.insert(outStock);
    }

    @Override
    public void publishOutStock(Long id, String outNum) {
        OutStock outStock = new OutStock();
        QueryWrapper<OutStockInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("out_num", outNum);
        List<OutStockInfo> outStockInfos = outStockInfoMapper.selectList(queryWrapper);
        if (!ObjectUtils.isEmpty(outStockInfos)) {
            for (OutStockInfo outStockInfo : outStockInfos) {
                String pNum = outStockInfo.getPNum();//物资编号
                Integer productNumber = outStockInfo.getProductNumber();//出库物资数
                QueryWrapper<ProductStock> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("p_num", pNum);
                ProductStock productStock = productStockMapper.selectOne(queryWrapper1);
                //仓库中已有这个物品
                if (!ObjectUtils.isEmpty(productStock)) {
                    productStock.setStock(productStock.getStock() - (long) productNumber);
                    productStockMapper.updateById(productStock);
                }
            }
            outStock.setId(id);
            outStock.setStatus(0);
            outStock.setCreateTime(new Date());
            outStockMapper.updateById(outStock);
        }
    }

    @Override
    public void delete(Long id, String outNum) {
        QueryWrapper<OutStockInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("out_num", outNum);
        outStockInfoMapper.delete(queryWrapper);
        outStockMapper.deleteById(id);
    }
}
